// inslude the SPI library:
#include <SPI.h>


// set pin 10 as the slave select for the digital pot:
const int chipSelect = 49;

void setup() {
  Serial.begin(9600);
  
  // set the chipSelect as an output:
  pinMode (chipSelect, OUTPUT);
  // initialize SPI:
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  
  // Configure chip
  spiReset();
}

void loop() {
  spiRead(0x0F);
  spiRead(0x1F);
  spiRead(0x2F);
  spiRead(0x3F);
  spiRead(0x4F);
  spiRead(0x5F);
  spiRead(0x6F);
  //spiWrite(0x0F, 0x80);
  Serial.println(" ");
  delay(500);
  //spiRead(0x0F);
  //spiWrite(0x0F, 0x0);
  //delay(500);
}

void spiReset() {
  // take the SS pin low to select the chip:
  digitalWrite(chipSelect,LOW);
  SPI.transfer(0xC0);  // RESET command b11000000
  // take the SS pin high to de-select the chip:
  digitalWrite(chipSelect,HIGH); 
}

void spiWrite(int address, int value) {
  // take the SS pin low to select the chip:
  digitalWrite(chipSelect,LOW);
  SPI.transfer(0x02); // WRITE command b00000010
  SPI.transfer(address);
  SPI.transfer(value);
  // take the SS pin high to de-select the chip:
  digitalWrite(chipSelect,HIGH); 
}

void spiRead(int address) {
  // take the SS pin low to select the chip:
  digitalWrite(chipSelect,LOW);
  SPI.transfer(0x03); // READ command b00000011
  SPI.transfer(address);
  int data = SPI.transfer(0x00);
  // take the SS pin high to de-select the chip:
  digitalWrite(chipSelect,HIGH);  
  Serial.print(data, BIN);
  Serial.print(", ");
}
